package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.dao.CustomerValueDao;
import com.gxa.dao.impl.CustomerDaoImpl;
import com.gxa.dao.impl.CustomerValueDaoImpl;
import com.gxa.pojo.Customer;
import com.gxa.service.CustomerValueService;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

public class CustomerValueServiceImpl implements CustomerValueService {
    //创建dao对象
    CustomerValueDao customerValueDao = new CustomerValueDaoImpl();

    /**
     * 查询客户价值信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult queryCustomer(Integer currentPage, Integer pageSize) {
        int param01 = (currentPage-1)*pageSize;
        int param02 = pageSize;
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            List<Customer> list = customerValueDao.queryCustomer(param01, param02);
            int count = 0;//总数据
            int consume_times = 0;//消费次数
            List orderList = null;//订单信息
            if(list!=null) {
                count = list.size();
                for (Customer customer : list) {
                    BigDecimal consumeTotalMoney = BigDecimal.valueOf(0);//消费总金额
                    //获取客户名
                    String customer_name = customer.getCustomer_name();
                    //获取客户所有的订单信息
                    orderList = customerValueDao.queryOrder(customer_name);
                    if(orderList.size() > 0) {
                        //根据客户名获取消费总金额
                        consumeTotalMoney = customerValueDao.getConsumeTotalMoney(customer_name);
                    }
                    //获取c对象里的消费总金额，并赋值给客户customer
                    customer.setConsume_total_money(consumeTotalMoney);
                    //存入客户对象
                    customer.setOrderList(orderList);
                    //获取消费次数并存入customer对象
                    consume_times = orderList.size();
                    customer.setConsume_times(consume_times);
                }
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setData(list);
                jsonResult.setCount(count);
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("查询失败");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 按指定条件选择客户价值信息
     * @param currentPage
     * @param pageSize
     * @param customer_name
     * @param type
     * @return
     */
    @Override
    public JsonResult searchCustomer(Integer currentPage, Integer pageSize, String customer_name, String type) {
        if(customer_name.isEmpty()){
            customer_name = null;
        }
        if(type.isEmpty()){
            type = null;
        }
        int param01 = (currentPage-1)*pageSize;
        int param02 = pageSize;
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            List<Customer> list = customerValueDao.searchCustomer(param01, param02, customer_name, type);
            int count = 0;//总数据
            int consume_times = 0;//消费次数
            List orderList = null;//订单信息
//            System.out.println(list.size());
            if(list!=null) {
                count = list.size();
                for (Customer customer : list) {
                    BigDecimal consumeTotalMoney = BigDecimal.valueOf(0);//消费总金额
                    //获取客户名
                    String name = customer.getCustomer_name();
                    //获取客户所有的订单信息
                    orderList = customerValueDao.queryOrder(name);
//                    System.out.println("orderList="+orderList.size());
//                    System.out.println("name=" + name);
                    if(orderList.size() > 0) {
                        //根据客户名获取消费总金额
                        consumeTotalMoney = customerValueDao.getConsumeTotalMoney(name);
                        //获取c对象里的消费总金额，并赋值给客户customer
                        customer.setConsume_total_money(consumeTotalMoney);
//                        System.out.println("consumeTotalMoney=" + consumeTotalMoney);
                    }
                    //获取c对象里的消费总金额，并赋值给客户customer
                    customer.setConsume_total_money(consumeTotalMoney);
                    //向客户存入订单信息
                    customer.setOrderList(orderList);
                    //获取消费次数并存入customer对象
                    consume_times = orderList.size();
                    customer.setConsume_times(consume_times);
                }
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setData(list);
                jsonResult.setCount(count);
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("查询失败");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 查询指定客户的所有订单信息
     * @param currentPage
     * @param pageSize
     * @param customer
     * @return
     */
    @Override
    public JsonResult queryCustomerOrder(Integer currentPage, Integer pageSize, String customer) {
        int param01 = (currentPage-1)*pageSize;
        int param02 = pageSize;
        //创建响应实体类对象
        JsonResult jsonResult = new JsonResult();
        try {
            //调用dao
            List list = customerValueDao.queryCustomerOrder(param01, param02, customer);
            int count = 0;
            if(list!=null){
                count = list.size();
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功");
                jsonResult.setData(list);
                jsonResult.setCount(count);
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("查询失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(501);
            jsonResult.setMsg("服务器开小差，请联系管理员");
        }
        return jsonResult;
    }
}
